﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;//添加命名空间，可以使用OleDbConnection等对象
using System.Data;//添加命名空间，可以使用dataset对象

public partial class adminstrator_planCourse : System.Web.UI.Page
{
    public OleDbConnection myConnection;
    public static readonly string sConnectionString = System.Configuration.ConfigurationManager.AppSettings["SQLConnString"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        myConnection = new OleDbConnection(sConnectionString);
        myConnection.Open();//打开数据库连接
        if (Session["UserName"] == null && Session["UserNo"] == null)//没有经过正常连接途径打开页面
            Page.ClientScript.RegisterStartupScript(this.GetType(), "", "<script type='text/javascript'>" +
                "alert('登录超时（或未登录），请您重新登陆！');parent.location.herf='../login.aspx'</script>", false);
        if (!IsPostBack)//page.IsPostBack是一个标志，当前请求是否第一次打开，IsPostBack只有第一次打的时候是false,其他时候是true
        {
            string courseName = Request.QueryString["courseName"];//获取页面传递过来的courseName值
            Label1.Text = "(" + courseName + ")";
            bind();//调用数据绑定函数
        }

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;//获取当前显示页的索引
        bind();//调用数据绑定函数
    }
    protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)/*点击“选择”按钮*/
    {
        string teacherNo = GridView1.DataKeys[e.NewEditIndex][0].ToString();//获取所编辑的行的索引，此处是获取DataKeys（主键）的教师工号tNo
        string teacherName = GridView1.DataKeys[e.NewEditIndex][1].ToString();//获取所编辑的行的索引，此处是获取DataKeys（主键）的教师工号tName
        string cNo = Request.QueryString["courseNo"];//获取页面传递过来的courseNo的值
        string mysql = "select * from ScoreTb where cNo='" + cNo + "'and sDegree is not null";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataReader myReader = mycmd.ExecuteReader();
        if (myReader .Read ())//存在记录
            Page .ClientScript .RegisterStartupScript (this.GetType (),"", "<script type='text/javascript'>alert('本门课程已经结束，不允许更换任课老师!').location.href='";
        else 
        {
            string mystr1="update CourseTb set tNo='"+ teacherNo + "'.tName='"+teacherName+"'where cNo='"+cNo+"'";//更新课程表
            mycmd =new OleDbCommand (mystr1,myConnection );//初始化mycmd
            mycmd .ExecuteNonQuery ();//执行
            string mystr2="update SelcourseTb set tNo='"+ teacherNo + "'.tName='"+teacherName+"'where cNo='"+cNo+"'";//更新选课表
            mycmd =new OleDbCommand (mystr2,myConnection );//初始化mycmd
            mycmd .ExecuteNonQuery ();//执行
            string mystr3="update ScoreTb set tNo='"+ teacherNo + "'.tName='"+teacherName+"'where cNo='"+cNo+"'";//更新成绩表
            mycmd =new OleDbCommand (mystr3,myConnection );//初始化mycmd
            mycmd .ExecuteNonQuery ();//执行
            Page .ClientScript .RegisterStartupScript (this .GetType (), "", "<script type='text/javascript'>alert('成功分配任课教师！').location.href='planCourse.aspx';";
        }
    }

    public void bind()
    {

        string mysql = "select * from CourseTb order by ID";
        OleDbCommand mycmd = new OleDbCommand(mysql, myConnection);
        OleDbDataAdapter Adapter = new OleDbDataAdapter();//OleDbDataAdapter表示要对access数据源执行sql语句存储过程
        Adapter.SelectCommand = mycmd;//获取或设置SQL语句存贮过程，用于选择数据源中的记录。（通俗来说，就是获取ScoreTb表的数据）
        DataSet myDS = new DataSet();//申明并初始化DataSet对象
        Adapter.Fill(myDS);//将数据填充到DataSet中。当OleDbDataAdapter填充到DataSet中时，他将为返回的数据创建适当的表和列
        if (myDS.Tables.Count > 0 && myDS.Tables[0].Rows.Count > 0)//判断myDS是不是空表，如果myDS至少存在1张表，并且表的行集合不为空
        {
            GridView1.DataSource = myDS;//装载数据源
            GridView1.DataKeyNames = new string[] { "sNo", "sName" };//设置主键，非常重要，关系到删除、编辑按钮的索引
            GridView1.DataBind();//绑定数据到显示列表
        }

    }


}